import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.Hashtable;

public class Main extends JFrame {

    public int time = 10;
    public JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 359999, time);
    //    current timer
    public Timer timer;
    public JButton startTimer = new JButton("Start");
    public JButton stopTimer = new JButton("Stop");
    public JButton setTimerRange = new JButton("Add new point");
    public JButton setCycleNumber = new JButton("Set cycle number");
    public JTextField labelMin1 = new JTextField("0");
    public JTextField labelMin2 = new JTextField("0");
    public JTextField labelSec1 = new JTextField("0");
    public JTextField labelSec2 = new JTextField("0");
    public JTextField labelMSec = new JTextField("00 ");
    public JSpinner timeSpinner = new JSpinner( new SpinnerDateModel() );

    public JTextField cycle = new JTextField("1");

    public  Hashtable labelTable = new Hashtable();

    boolean startPressed = false;

    Main(String title){
        super(title);
    }

    public void addComponentsToPane(Container container) {

        Panel labelPanel = new Panel();
        labelPanel.setLayout(new FlowLayout());
        container.add(labelPanel);
        labelPanel.add(labelMin1);
        labelPanel.add(labelMin2);
        labelPanel.add(new Label(":"));
        labelPanel.add(labelSec1);
        labelPanel.add(labelSec2);
        labelPanel.add(new Label(":"));
        labelPanel.add(labelMSec);

        Font newLabelFont = new Font(Font.SANS_SERIF, Font.ITALIC, 30);
        labelPanel.setFont(newLabelFont);
        container.setBackground(Color.WHITE);

        labelTable.put(new Integer(slider.getMinimum()), new JLabel("Min: 00:00:00"));
        labelTable.put(new Integer(slider.getMaximum()), new JLabel("Max: 59:59:99"));

        slider.setLabelTable(labelTable);

        slider.setPaintLabels(true);
       // slider.setMajorTickSpacing(60000);
        slider.setMinorTickSpacing(6000);
        slider.setPaintTicks(true);

        container.add(slider);

        Panel panel = new Panel();
        panel.setLayout(new GridLayout(0, 2));
        container.add(panel);
        panel.add(startTimer);
        panel.add(stopTimer);



        JSpinner.DateEditor timeEditor = new JSpinner.DateEditor(timeSpinner, "mm:ss");
        timeSpinner.setEditor(timeEditor);
        timeSpinner.setValue(new Date()); // will only show the current time
        panel.add(timeSpinner);
        panel.add( setTimerRange);
        panel.add(cycle);
        panel.add(setCycleNumber);


        bindActionListeners();
    }


    public void displayTime(int time, JTextField labelMin1, JTextField labelMin2, JTextField labelSec1, JTextField labelSec2, JTextField labelMSec) {
        int min1 = time / 60000;
        int min2 = (time - min1 * 60000) / 6000;
        int sec1 = (time - min1 * 60000 - min2 * 6000) / 1000;
        int sec2 = (time - min1 * 60000 - min2 * 6000 - sec1 * 1000) / 100;
        int millisec = time - min2 * 6000 - sec2 * 100 - min1 * 60000 - sec1 * 1000;
        try {
            if (Integer.parseInt(labelMin1.getText()) != min1) {
               labelMin1.setText(""+min1);
            }
            if (Integer.parseInt(labelMin2.getText()) != min2) {
               labelMin2.setText(""+min2);
            }
            if (Integer.parseInt(labelSec1.getText()) != sec1) {
               labelSec1.setText(""+sec1);
            }
            if (Integer.parseInt(labelSec2.getText()) != sec2) {
               labelSec2.setText(""+sec2);
            }
            labelMSec.setText(""+millisec);


        } catch (NumberFormatException e) {

        }
    }

    public void bindActionListeners() {
        startTimer.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if ((timer == null) || !timer.isRepeats()) {
                    timer = new Timer(10, new ActionListener() {
                        public void actionPerformed(ActionEvent ev) {
                            time -= 1;
                            if (time >= 0) {

                                displayTime(time,labelMin1,labelMin2, labelSec1, labelSec2, labelMSec);
                                slider.setValue(time);
                            }
                            if (timer.isRepeats()) {
                                timer.setRepeats(time > 0);
                            }
                        }
                    });
                    timer.start();
                }
            }
        });
        stopTimer.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (timer.isRepeats()) {
                    timer.setRepeats(false);
                }
            }
        });
        slider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                time = slider.getValue();
                displayTime(time, labelMin1, labelMin2, labelSec1, labelSec2, labelMSec);
            }
        });

    }

    private void createAndShowGUI() {
//        setDefaultLookAndFeelDecorated(true);

        setSize(new Dimension(500, 500));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(0, 1));

        //Set up the content pane and components in GridLayout
        addComponentsToPane(getContentPane());

        setLocationRelativeTo(null);

        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
               new Main("Sport timer").createAndShowGUI();
            }
        });
    }
}
